package com.lphj.datainterface.handle;

import com.lphj.datainterface.component.Config.ReadCustomerConfig;
import com.lphj.datainterface.entity.TCustomerType;
import com.lphj.datainterface.service.TCustomerTypeService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

import static org.apache.poi.ss.usermodel.Cell.CELL_TYPE_STRING;

@Component
public class TCustomerHandle {

    @Autowired
    TCustomerTypeService tCustomerTypeService;

    @Autowired
    ReadCustomerConfig readCustomerConfig;

    @Transactional
    public int addTCustomer(Sheet sheet) {
        int count = 0;
        boolean isNotNull = true;
        //获取标题
        Row firstRow = sheet.getRow(0);
        int index = 0;
        //获取首行标题
        List<String> firstRowTitle = new ArrayList<String>();

        if(sheet != null) {
            //获取首行标题
            while(isNotNull) {
                Cell cell = firstRow.getCell(index);
                if(cell != null) {
                    firstRowTitle.add(cell.getStringCellValue());
                } else {
                    isNotNull = false;
                }
                index ++;
            }
            //所属类型
            int rc_customerType=0;
            //商户ID
            int rc_gm_customerId=0;

            for(int j=0;j<firstRowTitle.size();j++) {
                if(firstRowTitle.get(j).equals(readCustomerConfig.getCustomerType())) {
                    rc_customerType = j;
                } else if(firstRowTitle.get(j).equals(readCustomerConfig.getCustomerId())) {
                    rc_gm_customerId = j;
                }
            }
            //创建单元格
            Cell cell;
            //清空TCustomerType表
            tCustomerTypeService.delTCustomerTypeByAll();
            for (int r = 1; r <= sheet.getLastRowNum(); r++) {
                //读取第r行
                Row row = sheet.getRow(r);
                if (row == null){
                    continue;
                }
                TCustomerType customerType = new TCustomerType();
                //获取商户所属类型
                cell = row.getCell(rc_customerType);
                if(cell != null) {
                    //cell.setCellType(CellType.STRING);
                    customerType.setsERP_customerNO(cell.getStringCellValue());
                }
                cell = null;
                //获取观麦商户ID
                cell = row.getCell(rc_gm_customerId);
                if(cell != null) {
                    customerType.setsOtherSystem_customerNO(cell.getStringCellValue());
                }
                cell = null;
                count += tCustomerTypeService.addTCustomerType(customerType);
            }
        }
        return count;
    }

    /**
     * 添加美菜客户
     * @param erpCustomerName
     * @param mcCustomerId
     * @return
     */
    @Transactional
    public int addTCustomer(String erpCustomerName,String mcCustomerId) {
        int count = 0;
        TCustomerType customerType = new TCustomerType();
        //ERP客户名称
        customerType.setsERP_customerNO(erpCustomerName);
        //美菜商户ID
        customerType.setsOtherSystem_customerNO(mcCustomerId);
        count += tCustomerTypeService.addTCustomerType(customerType);

        return count;
    }
}
